//
//  STALogView.m
//  STALog
//
//  Created by jerry on 2019/5/5.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STALogView.h"

@interface STALogView (){
    CGPoint startPoint;
}

@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIButton *clearButton;
@property (nonatomic, strong) UIButton *showButton;
@property (nonatomic, strong) UIButton *leftHideButton;
@property (nonatomic, strong) UIButton *rightHideButton;
@property (nonatomic, strong) UITextView *contentTextView;

@end

@implementation STALogView

+ (STALogView *)logView{
    NSInteger tag = 2019040300;
    STALogView *logView = [[UIApplication sharedApplication].keyWindow viewWithTag:tag];
    return logView;
}

+ (void)show{
    STALogView *view = [STALogView logView];
    if(!view){
        STALogView * logView = [[STALogView alloc] init];
        logView.backgroundColor = [UIColor whiteColor];
        logView.tag = 2019040300;
        [[UIApplication sharedApplication].keyWindow addSubview:logView];
        logView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height*2/3.0 , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/3.0 );
    }
}

+ (void)dismissView{
    STALogView *view = [STALogView logView];
    if(view){
        [view removeFromSuperview];
        view = nil;
    }
}

+ (void)addText:(NSString *)text{
    STALogView *view = [STALogView logView];
    if(view){
        NSString *log = [NSString stringWithFormat:@"%@ %@",view.contentTextView.text, text];
        view.contentTextView.text = log;
    }
}


- (instancetype)init{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    self.closeButton.frame = CGRectMake(8, 8, 40, 20);
    self.closeButton.layer.cornerRadius = 6.0;
    
    self.clearButton.frame = CGRectMake(8+48, 8, 40, 20);
    self.clearButton.layer.cornerRadius = 6.0;
    
    self.showButton.frame = CGRectMake(0, 0, 40, 20);
    
    self.leftHideButton.frame = CGRectMake(8+48*2, 8, 40, 20);
    self.leftHideButton.layer.cornerRadius = 6.0;
    
    self.rightHideButton.frame = CGRectMake(8+48*3, 8, 40, 20);
    self.rightHideButton.layer.cornerRadius = 6.0;
    
    self.contentTextView.frame = CGRectMake(8, 36, [UIScreen mainScreen].bounds.size.width - 16, [UIScreen mainScreen].bounds.size.height/3.0 - 44);
}

#pragma mark 展示按钮
- (void)actionShowButton{
    self.showButton.hidden = YES;
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height*2/3.0 , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/3.0 );
    } completion:^(BOOL finished) {
        self.closeButton.hidden = NO;
        self.clearButton.hidden = NO;
        self.leftHideButton.hidden = NO;
        self.rightHideButton.hidden = NO;
    }];
}

- (UIButton *)showButton{
    if(!_showButton){
        _showButton = [[UIButton alloc] init];
        [_showButton setTitle:@"展示" forState:UIControlStateNormal];
        _showButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _showButton.backgroundColor = [UIColor blueColor];
        [_showButton addTarget:self action:@selector(actionShowButton) forControlEvents:UIControlEventTouchUpInside];
        _showButton.hidden = YES;
        [self addSubview:_showButton];
    }
    return _showButton;
}
#pragma mark 右边隐藏

- (void)actionRightHideButton{
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 20 , 40, 20 );
    } completion:^(BOOL finished) {
        self.showButton.hidden = NO;
        self.closeButton.hidden = YES;
        self.clearButton.hidden = YES;
        self.leftHideButton.hidden = YES;
        self.rightHideButton.hidden = YES;
    }];
}

- (UIButton *)rightHideButton{
    if(!_rightHideButton){
        _rightHideButton = [[UIButton alloc] init];
        [_rightHideButton setTitle:@"右藏" forState:UIControlStateNormal];
        _rightHideButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _rightHideButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        [_rightHideButton addTarget:self action:@selector(actionRightHideButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightHideButton];
    }
    return _rightHideButton;
}

#pragma mark 左边隐藏

- (void)actionLeftHideButton{
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 20 , 40, 20 );
    } completion:^(BOOL finished) {
        self.showButton.hidden = NO;
        self.closeButton.hidden = YES;
        self.clearButton.hidden = YES;
        self.leftHideButton.hidden = YES;
        self.rightHideButton.hidden = YES;
    }];
}

- (UIButton *)leftHideButton{
    if(!_leftHideButton){
        _leftHideButton = [[UIButton alloc] init];
        [_leftHideButton setTitle:@"左藏" forState:UIControlStateNormal];
        _leftHideButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _leftHideButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        [_leftHideButton addTarget:self action:@selector(actionLeftHideButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftHideButton];
    }
    return _leftHideButton;
}

#pragma mark 关闭按钮
- (void)actionCloseButton{
    [STALogView dismissView];
}

- (UIButton *)closeButton{
    if(!_closeButton){
        _closeButton = [[UIButton alloc] init];
        [_closeButton setTitle:@"关闭" forState:UIControlStateNormal];
        _closeButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _closeButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        [_closeButton addTarget:self action:@selector(actionCloseButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeButton];
    }
    return _closeButton;
}

#pragma mark 清除数据按钮

- (void)actionClearButton{
    self.contentTextView.text = @"";
}

- (UIButton *)clearButton{
    if(!_clearButton){
        _clearButton = [[UIButton alloc] init];
        [_clearButton setTitle:@"清除" forState:UIControlStateNormal];
        _clearButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _clearButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        [_clearButton addTarget:self action:@selector(actionClearButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_clearButton];
    }
    return _clearButton;
}

#pragma mark展示内容按钮
- (UITextView *)contentTextView{
    if(!_contentTextView){
        _contentTextView = [[UITextView alloc] init];
        _contentTextView.font = [UIFont systemFontOfSize:13];
        _contentTextView.backgroundColor = [UIColor blackColor];
        _contentTextView.textColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        _contentTextView.editable = NO;
        [self addSubview:_contentTextView];
    }
    return _contentTextView;
}

@end

